package com.dzt.androidframework.network.mobapi.cook;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.network.mobapi.cook.Category.ResultBean.ChildsBeanX;
import com.dzt.androidframework.network.mobapi.cook.Category.ResultBean.ChildsBeanX.ChildsBean;

import java.util.List;
import java.util.Map;

/**
 * Created by M02323 on 2017/12/8.
 */

public class CookExpandableListAdapter extends BaseExpandableListAdapter {
	private Context context;
	private List<Map<String, ChildsBeanX>> categorys;
	private List<List<Map<String, ChildsBean>>> childs;
	private int groupId;
	private int childId;

	public CookExpandableListAdapter(Context context, int groupId,
									 int childId,
									 List<Map<String, ChildsBeanX>> groupList,
									 List<List<Map<String, ChildsBean>>> childList) {
		this.context = context;
		this.groupId = groupId;
		this.childId = childId;
		categorys = groupList;
		childs = childList;
	}

	@Override
	public int getGroupCount() {
		return categorys.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return childs.get(groupPosition).size();
	}

	@Override
	public ChildsBeanX getGroup(int groupPosition) {
		return categorys.get(groupPosition).get("group");
	}

	@Override
	public ChildsBean getChild(int groupPosition, int childPosition) {
		return childs.get(groupPosition).get(childPosition).get("child");
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(groupId, null);
		}
		TextView textView = (TextView) convertView.findViewById(R.id.tv_group);
		textView.setText(getGroup(groupPosition).getCategoryInfo().getName());
		return convertView;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition,
							 boolean isLastChild, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(childId, null);
		}
		TextView textView = (TextView) convertView.findViewById(R.id.tv_child);
		textView.setText(getChild(groupPosition, childPosition).getCategoryInfo().getName());
		return convertView;
	}

	@Override
	public boolean isChildSelectable(int i, int i1) {
		return true;
	}
}
